package com.dlb.andorid.presenter;

import com.dlb.andorid.XAPI;
import com.dlb.andorid.XContract;
import com.dlb.andorid.XPresenter;
import com.dlb.andorid.XServerMsg;
import com.dlb.andorid.bean.UserInfo;

import java.util.HashMap;

public class LoginPresenter extends XPresenter {


	public static int LOGIN_TYPE;

	public static final int LOGIN_TYPE_CODE = 1;
	public static final int LOGIN_TYPE_WX = 2;
	public static final int LOGIN_TYPE_PWD = 3;

	//	"type”:1    注册
//“type”:2    登录
//“type”:3    找回密码
//“type”:4    绑定手机
	public static final int CODE_TYPE_REG = 1;
	public static final int CODE_TYPE_LOGIN = 2;
	public static final int CODE_TYPE_FIND = 3;
	public static final int CODE_TYPE_BIND = 4;
	public static final int CODE_TYPE_NORMAL = 5;


	XContract.ILoginView iView;

	public LoginPresenter(XContract.ILoginView iView) {
		super(iView);
		this.iView = iView;
	}

	public void getCode(String phone, int ctype) {
		mModel.getCode(phone, ctype);
	}

	//手机+短信
	public void loginByCode(String phone, String code) {
		LOGIN_TYPE = LOGIN_TYPE_CODE;
		String arg = "{\"type\":" + LOGIN_TYPE_CODE + ",\"data\":{\"code\":\"" + code + "\",\"phone\":\"" + phone + "\"}}";
		mModel.login(arg);
	}

	public void loginByPwd(String phone, String pwd) {
		LOGIN_TYPE = LOGIN_TYPE_PWD;
		String arg = "{\"type\":" + LOGIN_TYPE_PWD + ",\"data\":{\"phone\":\"" + phone + "\",\"password\":\"" + pwd + "\"}}";
		mModel.login(arg);
	}

	public void loginByWx(String open_id) {
		LOGIN_TYPE = LOGIN_TYPE_WX;
		String arg = "{\"type\":" + LOGIN_TYPE_WX + ",\"data\":{\"open_id\":\"" + open_id + "\"}}";
		mModel.login(arg);
	}

	@Override
	public void onDataSuccess(int vocational_id, HashMap<String, Object> exData, Object data) {
		super.onDataSuccess(vocational_id, exData, data);
		switch (vocational_id) {
			case XAPI.GET_CODE_VOCATIONAL_ID:
				iView.getCodeSuccess(((XServerMsg) data).getMsg());
				break;
			case XAPI.LOGIN_VOCATIONAL_ID:
				iView.onLoginSuccess(((XServerMsg<UserInfo>) data).getData());
				break;
		}
	}
}
